package cn.iocoder.yudao.module.design.controller.admin.diary;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.diary.vo.*;
import cn.iocoder.yudao.module.design.convert.diary.DiaryConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.diary.DiaryDO;
import cn.iocoder.yudao.module.design.service.diary.DiaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 日记")
@RestController
@RequestMapping("/design/diary")
@Validated
public class DiaryController {

    @Resource
    private DiaryService diaryService;

    @PostMapping("/create")
    @Operation(summary ="创建日记")
    @PreAuthorize("@ss.hasPermission('design:diary:create')")
    public CommonResult<Long> createDiary(@Valid @RequestBody DiaryCreateReqVO createReqVO) {
        String url="{\"11\",\"2\",\"3\"}";
        return success(diaryService.createDiary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新日记")
    @PreAuthorize("@ss.hasPermission('design:diary:update')")
    public CommonResult<Boolean> updateDiary(@Valid @RequestBody DiaryUpdateReqVO updateReqVO) {
        diaryService.updateDiary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除日记")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:diary:delete')")
    public CommonResult<Boolean> deleteDiary(@RequestParam("id") Long id) {
        diaryService.deleteDiary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得日记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:diary:query')")
    public CommonResult<DiaryRespVO> getDiary(@RequestParam("id") Long id) {
        DiaryDO diary = diaryService.getDiary(id);
        return success(DiaryConvert.INSTANCE.convert(diary));
    }

    @GetMapping("/list")
    @Operation(summary ="获得日记列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:diary:query')")
    public CommonResult<List<DiaryRespVO>> getDiaryList(@RequestParam("ids") Collection<Long> ids) {
        List<DiaryDO> list = diaryService.getDiaryList(ids);
        return success(DiaryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得日记分页")
    @PreAuthorize("@ss.hasPermission('design:diary:query')")
    public CommonResult<PageResult<DiaryDO>> getDiaryPage(@Valid DiaryPageReqVO pageVO) {

        return success(diaryService.getDiaryPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出日记 Excel")
    @PreAuthorize("@ss.hasPermission('design:diary:export')")
    public void exportDiaryExcel(@Valid DiaryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DiaryDO> list = diaryService.getDiaryList(exportReqVO);
        // 导出 Excel
        List<DiaryExcelVO> datas = DiaryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "日记.xls", "数据", DiaryExcelVO.class, datas);
    }

    @PostMapping("/getDiaryPageResp")
    @Operation(summary ="获得日记列表分页")
    @PreAuthorize("@ss.hasPermission('design:diary:query')")
    public CommonResult< List<DiaryRespGroupVO>> getDiaryPageResp(@Valid @RequestBody  DiaryPageReqVO pageVO) {
        return success(diaryService.getDiaryPageResp(pageVO));
    }
}
